package com.spring.reactive;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.Arrays;

/**
 * @Author: TongRui乀
 * @Date: 2021/3/29 23:23
 * @description：
 */
public class CreateApiTest {

    public static void test_01(){

        Flux.just(1,2,3).subscribe(System.out::println);

    }

    public static void test_02(){

        Flux.from(subscriber->{
            subscriber.onNext(1);
            subscriber.onNext(2);
            subscriber.onNext(3);
            subscriber.onComplete();
        }).subscribe(System.out::println);

    }

    public static void test_03(){

        Flux.fromIterable(Arrays.asList(1,2,3)).subscribe(System.out::println);

    }

    public static void test_04(){

        Flux.range(0, 10).subscribe(System.out::println);

    }

    public static void test_05(){
        Flux.interval(Duration.ofMillis(2000), Duration.ofMillis(1000)).subscribe(System.out::println);
    }

    public static void test_06(){
        Flux.generate(sink->{
            sink.next(1);
            // 停止
            sink.complete();
        }).subscribe(System.out::println);

        Flux.generate(()->1, (i, sink)->{
            sink.next(i++);
            return i;
        }).subscribe(System.out::println);
    }

    public static void test_07(){
        Flux.create(emitter->{
            emitter.next(1);
            emitter.next(2);
            emitter.complete();
        }).subscribe(System.out::println);
    }

    public static void test_08(){

        Mono.create(monoSink -> {
            monoSink.success(1);
        }).subscribe(System.out::println);

    }

    public static void main(String[] args) {

        test_07();

        while (true){}
    }

}
